<!DOCTYPE html>
<html>
<head>
    <title>部分路线规划</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            margin: 0;
            padding: 10px;
            background-color: #f5f6fa;
        }

        .container {
            width: 100%;
            max-width: 1200px;
            margin: 0 auto;
            text-align: center;
            padding: 0 10px;
            box-sizing: border-box;
            display: flex;
            flex-direction: column;
            gap: 10px;
        }

        h1 {
            color: #2c3e50;
            margin: 10px 0;
            font-size: 20px;
        }

        .map-container {
            margin: 15px auto;
            width: 100%;
            max-width: 800px;
            background: white;
            padding: 10px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            box-sizing: border-box;
        }

        .map-image {
            width: 100%;
            height: auto;
            border-radius: 5px;
        }

        .button-grid {
            display: flex;
            flex-direction: column;
            gap: 15px;
            margin: 20px auto;
            width: 100%;
            max-width: 800px;
            padding: 0;
            box-sizing: border-box;
        }

        .button-row {
            display: flex;
            gap: 15px;
            justify-content: space-between;
            width: 100%;
            margin: 0;
        }

        .attraction-button {
            background-color: #c5e4f3;
            color: #2c3e50;
            border: none;
            padding: 16px 10px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 32px;
            font-weight: bold;
            transition: all 0.3s ease;
            width: calc(50% - 0px);
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            border: 1px solid #9ccde4;
            margin: 0;
        }

        .attraction-button:hover {
            background-color: #a8d5ea;
            transform: translateY(-2px);
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .return-button {
            background-color: #e74c3c;
            color: white;
            border: none;
            padding: 12px 30px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 24px;
            font-weight: bold;
            text-decoration: none;
            transition: all 0.3s ease;
        }

        .return-button:hover {
            background-color: #c0392b;
            transform: translateY(-2px);
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .prompt {
            background-color: #fff3cd;
            color: #856404;
            padding: 12px;
            border-radius: 5px;
            margin: 15px auto;
            width: 100%;
            max-width: 800px;
            text-align: center;
            box-sizing: border-box;
            font-size: 18px;
            font-weight: bold;
        }

        .prompt-text {
            color: #856404;
            background-color: #fff3cd;
            padding: 10px;
            border-radius: 5px;
            margin-bottom: 15px;
            text-align: center;
            font-weight: bold;
        }

        .route-info {
            background: white;
            padding: 12px 15px;
            margin: 10px auto;
            border-radius: 5px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            width: 100%;
            max-width: 800px;
            text-align: left;
            box-sizing: border-box;
            line-height: 1.3;
            position: relative;
            padding-bottom: 15px;
            display: block;  /* 确保初始状态显示 */
        }

        .route-info p {
            margin: 5px 0;
        }

        .route-info strong {
            margin-right: 8px;
        }

        .button-container {
            display: flex;
            justify-content: flex-end;
            gap: 15px;
            margin-top: 15px;
        }

        .reset-button {
            background-color: #3498db;
            color: white;
            border: none;
            padding: 12px 30px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 24px;
            font-weight: bold;
            text-decoration: none;
            transition: all 0.3s ease;
        }

        .reset-button:hover {
            background-color: #2980b9;
            transform: translateY(-2px);
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        /* 移动设备适配 */
        @media (max-width: 768px) {
            .container {
                padding: 5px;
            }

            .button-grid {
                gap: 10px;
            }

            .button-row {
                gap: 10px;
            }

            .attraction-button {
                font-size: 28px;
                padding: 12px 8px;
            }

            .return-button {
                font-size: 20px;
                padding: 10px 25px;
            }

            .button-container {
                flex-direction: row;
                justify-content: flex-end;
            }
        }

        /* 超小屏幕适配 */
        @media (max-width: 320px) {
            .attraction-button {
                font-size: 16px;
                padding: 12px 6px;
            }

            .return-button {
                font-size: 18px;
                padding: 8px 20px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>动物园导览图</h1>
        
        <div class="map-container">
            <img id="mapImg" src="data:image/png;base64,{{ initial_map }}" class="map-image" alt="地图">
        </div>

        <div id="result" class="route-info">
            <div>
                <p id="promptText" class="prompt-text">请选择起点和终点</p>
                <div class="button-container">
                    <a href="/" class="return-button">返回主菜单</a>
                </div>
            </div>
        </div>

        <div class="button-grid">
            <div class="button-row">
                <button class="attraction-button" onclick="selectAttraction('金丝猴馆')">金丝猴馆</button>
                <button class="attraction-button" onclick="selectAttraction('鹿苑')">鹿苑</button>
            </div>
            <div class="button-row">
                <button class="attraction-button" onclick="selectAttraction('两栖爬行馆')">两栖爬行馆</button>
                <button class="attraction-button" onclick="selectAttraction('科普馆')">科普馆</button>
            </div>
            <div class="button-row">
                <button class="attraction-button" onclick="selectAttraction('火烈鸟馆')">火烈鸟馆</button>
                <button class="attraction-button" onclick="selectAttraction('企鹅乐园')">企鹅乐园</button>
            </div>
            <div class="button-row">
                <button class="attraction-button" onclick="selectAttraction('非洲动物区')">非洲动物区</button>
                <button class="attraction-button" onclick="selectAttraction('儿童动物园')">儿童动物园</button>
            </div>
            <div class="button-row">
                <button class="attraction-button" onclick="selectAttraction('猿猴馆')">猿猴馆</button>
                <button class="attraction-button" onclick="selectAttraction('长颈鹿馆')">长颈鹿馆</button>
            </div>
        </div>
    </div>

    <script>
        let selectedStart = null;
        let selectedEnd = null;

        function selectAttraction(attraction) {
            if (!selectedStart) {
                selectedStart = attraction;
                
                // 高亮显示选中的起点按钮
                document.querySelectorAll('.attraction-button').forEach(button => {
                    if (button.textContent === attraction) {
                        button.style.backgroundColor = '#2ecc71';
                    }
                });
            } else if (!selectedEnd) {
                if (attraction === selectedStart) {
                    alert('终点不能与起点相同');
                    return;
                }
                selectedEnd = attraction;
                
                // 高亮显示选中的终点按钮
                document.querySelectorAll('.attraction-button').forEach(button => {
                    if (button.textContent === attraction) {
                        button.style.backgroundColor = '#e74c3c';
                    }
                });
                
                // 计算路线
                calculateRoute();
            }
        }

        function calculateRoute() {
            fetch('/calculate_route', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    start: selectedStart,
                    end: selectedEnd,
                    type: 'partial'
                })
            })
            .then(response => response.json())
            .then(data => {
                if (data.status === 'success') {
                    const resultDiv = document.getElementById('result');
                    resultDiv.style.display = 'block';
                    resultDiv.innerHTML = `
                        <div>
                            <p><strong>推荐路线：</strong>${data.path.join(' → ')}</p>
                            <p><strong>总距离：</strong>${data.distance} 米</p>
                            <p><strong>预计用时：</strong>${Math.round(data.distance/50 + data.path.length * 10)}分钟</p>
                            <div class="button-container">
                                <button onclick="resetSelection()" class="reset-button">重新选择</button>
                                <a href="/" class="return-button">返回主菜单</a>
                            </div>
                        </div>
                    `;
                    
                    // 更新地图
                    const mapImg = document.getElementById('mapImg');
                    mapImg.src = `data:image/png;base64,${data.map}`;
                } else {
                    const resultDiv = document.getElementById('result');
                    resultDiv.style.display = 'block';
                    resultDiv.innerHTML = `
                        <div class="error">
                            计算路线失败：${data.message || '未知错误'}
                        </div>
                        <a href="/" class="return-button">返回主菜单</a>
                    `;
                    resetSelection();
                }
            })
            .catch(error => {
                console.error('Error:', error);
                const resultDiv = document.getElementById('result');
                resultDiv.style.display = 'block';
                resultDiv.innerHTML = `
                    <div class="error">
                        请求失败，请稍后重试
                    </div>
                    <a href="/" class="return-button">返回主菜单</a>
                `;
                resetSelection();
            });
        }

        function resetSelection() {
            selectedStart = null;
            selectedEnd = null;
            
            // 重置按钮颜色
            document.querySelectorAll('.attraction-button').forEach(button => {
                button.style.backgroundColor = '#c5e4f3';  // 使用原始背景色
            });
            
            // 重置结果显示为初始状态
            const resultDiv = document.getElementById('result');
            resultDiv.innerHTML = `
                <div>
                    <p id="promptText" class="prompt-text">请选择起点和终点</p>
                    <div class="button-container">
                        <a href="/" class="return-button">返回主菜单</a>
                    </div>
                </div>
            `;
            
            // 重置地图
            fetch('/calculate_route', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({})
            })
            .then(response => response.json())
            .then(data => {
                if (data.status === 'success') {
                    const mapImg = document.getElementById('mapImg');
                    mapImg.src = `data:image/png;base64,${data.map}`;
                }
            });
        }

        // 页面加载时更新人流量数据
        fetch('/update_crowd_data')
            .then(response => response.json())
            .then(data => {
                if (data.status !== 'success') {
                    console.error('更新人流量数据失败：', data.message);
                }
            });
    </script>
</body>
</html>